import { remote } from "webdriverio";
import { readFileSync } from "fs";
import Concert from "./damaiWeb";

async function main() {
  const browser = await remote({
    capabilities: {
      browserName: "chrome",
      "goog:chromeOptions": {
        args: ["--disable-blink-features=AutomationControlled"],
        prefs: {
          "profile.managed_default_content_settings.images": 2,
          "profile.managed_default_content_settings.javascript": 1,
          "permissions.default.stylesheet": 2,
        },
        // mobileEmulation: { deviceName: "Nexus 6" },
      },
      "wdio:chromedriverOptions": {
        binary: "./chromedriver-win64/chromedriver.exe",
      },
      pageLoadStrategy: "eager",
    },
  });

  const config = JSON.parse(readFileSync("./config/config.json", "utf8"));

  const concert = new Concert(browser, config);

  try {
    await concert.enterConcert();
  } catch (e) {
    console.error(e);
    process.exit(1);
  }

  try {
    await concert.chooseTicket();
    await concert.checkOrder();
  } catch (e) {
    await browser.url(concert["target_url"]);
    console.error(e);
  }

  if (concert["status"] === 6) {
    console.log(
      `###经过${concert["num"]}轮奋斗，共耗时${concert["time_end"]}秒，抢票成功！###`
    );
  }

  await browser.deleteSession(); // 关闭浏览器
}

main();
